''' <summary>
''' This is the placeholder for all of the dashboard controls.
''' The home page loads a repeater that retrieves the list of dashboard items
''' to display for the current user's role.  The item template holds a DashboardPlaceHolder(this class)
''' control, and assigns a control name and title.  This class is used to dynamically load the underlying controls.
''' </summary>
''' <remarks></remarks>
Partial Class DashboardControls_DashboardPlaceHolder
    Inherits System.Web.UI.UserControl
    ''' <summary>
    ''' The .ascx Name of the control to load in this place holder.  When set, the control
    ''' is dynamically loaded.  When called, the string name of the control is returned.
    ''' </summary>
    ''' <value></value>
    ''' <returns>the string name of the control</returns>
    ''' <remarks></remarks>
    Public Property ControlName() As String
        Get
            If Me.DashboardPlaceholderPanel.Controls.Count > 0 Then
                Return Me.DashboardPlaceholderPanel.Controls(0).GetType().FullName
            Else
                Return ""
            End If
        End Get
        Set(ByVal value As String)
            ClearPanelControls()
            If (value.Equals("")) Then Exit Property
            Dim newCtl As System.Web.UI.Control = Me.LoadControl(value)
            Me.DashboardPlaceholderPanel.Controls.Add(newCtl)
            Me.EmbeddedControl = newCtl
        End Set
    End Property
    ''' <summary>
    ''' The Control object in the DashboardControls foder that is loaded into this control.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Property EmbeddedControl() As System.Web.UI.Control
        Get
            Return Session(Me.GetType().FullName & "_embeddedControl")
        End Get
        Set(ByVal value As System.Web.UI.Control)
            Session(Me.GetType().FullName & "_embeddedControl") = value
            SetEmbeddedTitle()
        End Set
    End Property

    ''' <summary>
    ''' Checks if the page needs to create the embedded control
    ''' </summary>
    ''' <returns>true if this control has an embedded control loaded in the panel, false otherwise.</returns>
    ''' <remarks></remarks>
    Private Function ControlExists() As Boolean
        If EmbeddedControl Is Nothing Then
            Return False
        Else
            If Me.DashboardPlaceholderPanel.Controls.IndexOf(EmbeddedControl) > -1 Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
    ''' <summary>
    '''  The title to pass to the embedded control
    ''' </summary>
    ''' <value></value>
    ''' <returns>The title string shown at the top of the embedded control.</returns>
    ''' <remarks></remarks>
    Public Property Title() As String
        Get
            Return ViewState("title")
        End Get
        Set(ByVal value As String)
            ViewState("title") = value
            SetEmbeddedTitle()
        End Set
    End Property

    ''' <summary>
    ''' Sets the embedded control title property.  Needs to make sure that the control has been added
    ''' before attempting to set the property.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetEmbeddedTitle()
        Try
            If Not EmbeddedControl Is Nothing Then
                Dim dsh As IDashBoard = EmbeddedControl
                dsh.Title = Me.Title
            End If
        Catch ex As Exception
            'do nothing just exit
        End Try
    End Sub

    ''' <summary>
    ''' Clears the control, in case a control needs to be replaced.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearPanelControls()
        Dim i As Integer
        For i = Me.DashboardPlaceholderPanel.Controls.Count - 1 To 0 Step -1
            Me.DashboardPlaceholderPanel.Controls.Remove(Me.DashboardPlaceholderPanel.Controls(i))
        Next
        Me.EmbeddedControl = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.IsPostBack Then
            SetControl()
            Dim dshControl As IDashBoard = EmbeddedControl
            dshControl.LoadControl(sender, e)
        End If
    End Sub

    ''' <summary>
    ''' If control has been assigned, but not added to the panel, add it.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetControl()
        If ControlExists() = False Then

            If Not EmbeddedControl Is Nothing Then
                SetEmbeddedTitle()
                Me.DashboardPlaceholderPanel.Controls.Add(EmbeddedControl)
            End If

        End If

    End Sub

End Class
